package com.jiangore.config;

import com.jiangore.ws.AuthInterceptor;
import com.jiangore.ws.IHelloWebService;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

/**
 * @Todo
 * @Author jiangtao
 * @CreateBy 2020-03-06 8:25 PM
 * @UpdateBy jiangtao
 * @Version v1.0
 */
@Configuration
public class WebServiceConfig {

    @Autowired
    private IHelloWebService helloWebService;

    //@Autowired
    ///private AuthInterceptor interceptor;

    @Bean(name = "cxfServlet")
    public ServletRegistrationBean dispatcherServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/ws/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    /* 注册服务hello */
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), helloWebService);

        endpoint.publish("/hello");
        //添加校验拦截器
        endpoint.getInInterceptors().add(new AuthInterceptor());

        return endpoint;
    }
}
